﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// Qt headers
#include <QPushButton>
#include <QLabel>
#include <QFrame>
#include <QHBoxLayout>
#include <QVariant>

// custom headers
#include "Bubble.h"
#include "GbmpNew.h"
#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    Bubble::Bubble(QString caption, QString hiddenText, QString &identifier, QWidget *parent)
        : QWidget(parent), 
        m_caption(caption),
        m_hiddenText(hiddenText),
        m_identifier(identifier)
    {
        Init();
    }

    Bubble::~Bubble()
    {
    }

    void Bubble::enterEvent(QEvent * event)
    {
        if (event)
        {
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblCaption, L"m_pLblCaption为空", L"GMEP", L"2024-01-30");
            m_pLblCaption->setStyleSheet(QString(
                "QLabel#%1{"
                "font-family:\"Microsoft YaHei\";"
                "font-size:12px;"
                "color:rgba(0,0,0,255);"
                "}"
            ).arg(m_pLblCaption->objectName()));
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pMainFrame, L"m_pMainFrame为空", L"GMEP", L"2024-01-30");
            m_pMainFrame->setStyleSheet(QString(
                "QFrame#%1{"
                "border:1px solid rgba(82,145,221,255);"
                "border-radius:2px;"
                "background-color:rgba(45, 175, 205, 255);"
                "}"
            ).arg(m_pMainFrame->objectName()));
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblText, L"m_pLblText为空", L"GMEP", L"2024-01-30");
            m_pLblText->setStyleSheet(QString(
                "QLabel#%1{"
                "font-family:\"Microsoft YaHei\";"
                "font-size:12px;"
                "color:rgba(255,255,255,255);"
                "}"
            ).arg(m_pLblText->objectName()));
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBtnBreak, L"m_pBtnBreak为空", L"GMEP", L"2024-01-30");
            m_pBtnBreak->setIcon(QIcon(":/images/icon_close_hover.png"));
        }
    }

    void Bubble::leaveEvent(QEvent * event)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblCaption, L"m_pLblCaption为空", L"GMEP", L"2024-01-30");
        m_pLblCaption->setStyleSheet(QString(
            "QLabel#%1{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(120,122,131,255);"
            "}"
        ).arg(m_pLblCaption->objectName()));
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pMainFrame, L"m_pMainFrame为空", L"GMEP", L"2024-01-30");
        m_pMainFrame->setStyleSheet(QString(
            "QFrame#%1{"
            "border:1px solid rgba(82,145,221,255);"
            "border-radius:2px;"
            "background-color:rgba(255,255,255,255);"
            "}"
        ).arg(m_pMainFrame->objectName()));
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblText, L"m_pLblText为空", L"GMEP", L"2024-01-30");
        m_pLblText->setStyleSheet(QString(
            "QLabel#%1{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(82,145,221,255);"
            "}"
        ).arg(m_pLblText->objectName()));
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBtnBreak, L"m_pBtnBreak为空", L"GMEP", L"2024-01-30");
        m_pBtnBreak->setIcon(QIcon(":/images/icon_close_normal.png"));
    }

    void Bubble::Init()
    {
        // main layout
        QHBoxLayout *pMainLayout = NEW_AS_QT_CHILD(QHBoxLayout, this);
        setLayout(pMainLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pMainLayout, L"pMainLayout为空", L"GMEP", L"2024-01-30");
        pMainLayout->setContentsMargins(0, 0, 0, 0);
        pMainLayout->setSpacing(0);

        // main frame
        m_pMainFrame = NEW_AS_QT_CHILD(QFrame,this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pMainFrame, L"m_pMainFrame为空", L"GMEP", L"2024-01-30");
        m_pMainFrame->setObjectName(QString::fromStdWString(L"BubbleMainFrame_%1").arg(m_identifier));
        pMainLayout->addWidget(m_pMainFrame);
        m_pMainFrame->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
        m_pMainFrame->setStyleSheet(QString("QFrame#%1{"
            "border:1px solid rgba(82,145,221,255);"
            "border-radius:2px;"
            "background-color:rgba(255,255,255,255);"
            "}"
        ).arg(m_pMainFrame->objectName()));

        // content layout
        QHBoxLayout *pContentLayout = NEW_AS_QT_CHILD(QHBoxLayout, m_pMainFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pContentLayout, L"pContentLayout为空", L"GMEP", L"2024-01-30");
        m_pMainFrame->setLayout(pContentLayout);
        pContentLayout->setContentsMargins(8, 0, 8, 0);
        pContentLayout->setSpacing(5);

        // caption
        m_pLblCaption = NEW_AS_QT_CHILD(QLabel,m_caption, m_pMainFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblCaption, L"m_pLblCaption为空", L"GMEP", L"2024-01-30");
        m_pLblCaption->setObjectName(QString::fromStdWString(L"BubbleCaptionLabel_%1").arg(m_identifier));
        pContentLayout->addWidget(m_pLblCaption);
        m_pLblCaption->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
        m_pLblCaption->setStyleSheet(QString(
            "QLabel#%1{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(120,122,131,255);"
            "}"
        ).arg(m_pLblCaption->objectName()));

        // text
        m_pLblText = NEW_AS_QT_CHILD(QLabel,m_pMainFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblText, L"m_pLblText为空", L"GMEP", L"2024-01-30");
        m_pLblText->setObjectName(QString::fromStdWString(L"BubbleTextLabel_%1").arg(m_identifier));
        pContentLayout->addWidget(m_pLblText);
        m_pLblText->setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Fixed);
        m_pLblText->setStyleSheet(QString(
            "QLabel#%1{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(82,145,221,255);"
            "}"
        ).arg(m_pLblText->objectName()));

        // break button
        m_pBtnBreak = NEW_AS_QT(QPushButton,QIcon(":/images/icon_close_normal.png"), QString(), m_pMainFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBtnBreak, L"m_pBtnBreak为空", L"GMEP", L"2024-01-30");
        m_pBtnBreak->setObjectName(QString::fromStdWString(L"BubbleBreakButton_%1").arg(m_identifier));
        pContentLayout->addWidget(m_pBtnBreak);
        m_pBtnBreak->setCursor(QCursor(Qt::CursorShape::PointingHandCursor));
        m_pBtnBreak->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
        m_pBtnBreak->setStyleSheet(QString(
            "QPushButton#%1{"
            "background-color:rgba(0,0,0,0);"
            "}"
        ).arg(m_pBtnBreak->objectName()));
        connect(m_pBtnBreak, &QPushButton::clicked, this, &Bubble::Break);

        // bubble self
        setFixedHeight(24);
        CheckHidden();
    }

    void Bubble::CheckHidden()
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblText, L"m_pLblText为空", L"GMEP", L"2024-01-30");
        bool visible = m_pLblText->text() != m_hiddenText;
        setVisible(visible);
        emit VisibleChanged(false);
    }

    void Bubble::Break()
    {
        setVisible(false);
        emit VisibleChanged(false);
        emit Broken();
    }

    void Bubble::SetText(QString text)
    {
        text = text.replace(QString::fromStdWString(L"\r"), QString::fromStdWString(L" ")).replace(QString::fromStdWString(L"\n"), QString::fromStdWString(L" "));
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblText, L"m_pLblText为空", L"GMEP", L"2024-01-30");
        if (text != m_pLblText->text())
        {
            m_pLblText->setText(text);
            CheckHidden();
        }
    }
}